<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>


	<script type="text/javascript">
		function goPage(pageIndex,pageSize) {
            //搜索表单,动态修改action属性
            $("#searchForm").prop("action","itemcat_page?pageIndex="+pageIndex+"&pageSize="+pageSize);
            //当表单提交，那么表单中的参数也会一起提交
            $("#searchForm").submit();
        }

        function queryChilds(parent_id) {
			//表单中隐藏的父级ID修改
			$("#parent_id").val(parent_id);
			$("#searchForm").prop("action","itemcat_page?pageIndex=1&pageSize=5");
			$("#searchForm").submit();
        }
        
        function getTypeTemplates() {
			//异步加载新增页面上需要的模板下拉框数据
			$.getJSON("typeTemplate_all",null,function (result) {
//				alert(JSON.stringify(result));
				//绑之前需要清空，不然会一直在下面追加
				$("#select_type_add").html("");
				for(var i=0;i<result.length;i++){
				    $("#select_type_add").append("<option value='"+result[i].id+"'>"+result[i].name+"</option>");
				}

				var lis = $("#type_parent li");
				var str = "";
				for(var i=0;i<lis.length;i++){
				    var t = $(lis[i]).text();
				    if(i==lis.length-1){
				        str = str+t;
					}else{
                        str = str+t+"<<";
                    }
				}
				$("#templateName_add").text(str);
            });
        }
        //新增
        function addItemCat() {
		    var data = $("#addForm").serialize();
		    $.ajax({
				url:"addItemCat",
				data:data,
				type:"post",
				dataType:"json",
                success:function (result) {
                    //alert(JSON.stringify(result));
                    if(result.code==200){
                        alert(result.message);
                        location.href="itemcat_page";
                    }else{
                        alert(result.message);
                    }
                }
			});
        }

        //修改页面
		function goUpdate(id) {
//		    alert(id);
		    //异步加载更新页面上需要的模板的数据
			$.getJSON("typeTemplate_goupdate",{"id":id},function (result) {
                alert(JSON.stringify(result));
			    var parent_id = result.parent_id;
			    var name = result.name;
			    var id = result.id;
			    $("#id_update").val(id);
                $("#name_update").val(name);
                $("#parent_id_update").val(parent_id);

                //异步加载下拉框
				$.getJSON("typeTemplate_all",null,function (result) {
				    $("#select_type_update").html("");
				    for(var i=0;i<result.length;i++){
                        $("#select_type_update").append("<option value='" + result[i].id + "'>" + result[i].name + "</option>");
                    }

                });
                //回显下拉框
                $("#select_type_update").val(result.typeTemplate.id);

                var lis=$("#type_parent li");
                var str="";
                for(var i=0;i<lis.length;i++){
                    var t=$(lis[i]).text();
                    if(i==lis.length-1){
                        str=str+t;
                    }else{
                        str=str+t+"<<";
                    }
                }
                //alert(str);
                //截图最后的<<
                $("#templateName_update").text(str);
            });
        }

        function updateItemCat() {
		    var data = $("#updateForm").serialize();
			$.ajax({
				url:"updateItemCat",
				data:data,
				type:"post",
				dataType:"json",
				success:function (result) {
//                    alert(JSON.stringify(result));
                    if(result.code==200){
                        alert(result.message);
                        location.href="itemcat_page";
                    }else{
                        alert(result.message);
                    }
                }
			});
        }
	</script>
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
  <form id="searchForm" action="itemcat_page?pageIndex=1&pageSize=5" method="post">
	  <!--父级id-->
	  <input type="hidden" id="parent_id" name="parent_id" th:value="${parent_id}" />
  </form>
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理
                       	</h3>
                    </div>

                    <div class="box-body">
                  			 <ol class="breadcrumb" th:utext="${str}" id="type_parent">
                        		<!--<li>-->
		                        	<!--<a href="#" >顶级分类列表</a> -->
		                        <!--</li>-->
		                        <!--<li>-->
		                       		<!--<a href="#" >珠宝</a>-->
		                        <!--</li>-->
		                        <!--<li>-->
		                        	<!--<a href="#" >银饰</a>-->
		                        <!--</li>-->
	                        </ol>

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" onclick="getTypeTemplates();" data-target="#addModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" ><i class="fa fa-check"></i> 刷新</button>

                                    </div>
                                </div>
                            </div>


			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input type="checkbox" class="icheckbox_square-blue">
			                              </th>
										  <th class="sorting_asc">分类ID</th>
									      <th class="sorting">分类名称</th>
									      <th class="sorting">类型模板ID</th>

					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody>
			                          <tr th:each="itemCat:${pageUtils.records}">
			                              <td><input  type="checkbox" ></td>
				                          <td th:text="${itemCat.id}">982</td>
									      <td th:text="${itemCat.name}">吊坠/项链</td>
									      <td th:text="${itemCat.typeTemplate.name}">
									      	11
									      </td>
		                                  <td class="text-center">
		                                      <button type="button" class="btn bg-olive btn-xs" th:if="${flag==false}" th:onclick="|queryChilds(${itemCat.id})|">查询下级</button>
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" th:onclick="|goUpdate(${itemCat.id})|">修改</button>
											  <!--<button type="button" class="btn bg-red btn-xs" data-toggle="modal" th:onclick="|goUpdate(${itemCat.id})|" >删除</button>-->
										  </td>
			                          </tr>
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->
							<!-- 分页组件 -->
							<div class="row">
								<div class="col-sm-6"><div class="dataTables_paginate paging_bootstrap" id="sample-table_paginate">
									<ul class="pagination">
										<li class="prev"><a th:href="|javascript:goPage(1,${pageUtils.pageSize});|">首页</a></li>
										<!--<li class="prev" th:if="${pageUtils.pageIndex>1}"><a th:href="|brand_page?pageIndex=${pageUtils.pageIndex-1}&pageSize=${pageUtils.pageSize}|">上一页</a></li>-->

										<li class="next" th:each="num:${pageUtils.numbers}" th:class="${pageUtils.pageIndex==num}? active"><a th:href="|javascript:goPage(${num},${pageUtils.pageSize});|" th:text="${num}">XXX</a></li>

										<!--	<li class="next" th:if="${pageUtils.pageIndex<pageUtils.pageCount}"><a th:href="|brand_page?pageIndex=${pageUtils.pageIndex+1}&pageSize=${pageUtils.pageSize}|">下一页</a></li>-->
										<li class="next"><a th:href="|javascript:goPage(${pageUtils.pageCount},${pageUtils.pageSize});|">尾页</a></li>
										<li class="next"><a href="#" th:text="|当前${pageUtils.pageIndex}/${pageUtils.pageCount}页|"></a></li>
										<li class="next"><a href="#" th:text="|共${pageUtils.totalCount}条|">XXX</a></li>
									</ul>
								</div>
								</div>
							</div>
							<!-- 分页组件 -->
                        </div>
                        <!-- 数据表格 /-->




                     </div>
                    <!-- /.box-body -->


<!-- 编辑窗口 -->
  <form id="updateForm" action="#" method="post">
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品分类编辑</h3>
		</div>
		<div class="modal-body">

			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>上级商品分类</td>
		      		<td id="templateName_update">
		      		   <!--珠宝 >>  银饰-->
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>商品分类名称</td>
		      		<td>
						<input type="hidden" name="id" id="id_update">
						<input class="form-control" name="parent_id" type="hidden" id="parent_id_update">
						<input  class="form-control" name="name" id="name_update" placeholder="商品分类名称">
					</td>
		      	</tr>
		      	<tr>
		      		<td>类型模板</td>
		      		<td>
						<select id="select_type_update" name="typeTemplate.id" class="form-control"></select>
		      			<!--<input select2 config="options['type_template']" placeholder="商品类型模板" class="form-control" type="text"/>-->
		      		</td>
		      	</tr>
			 </table>

		</div>
		<div class="modal-footer">
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateItemCat();">修改</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
  </form>
<form id="addForm" action="#" method="post">
  <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel1">商品分类新增</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>上级商品分类</td>
						  <td id="templateName_add">
							  <!--珠宝 >>  银饰-->
						  </td>
					  </tr>
					  <tr>
						  <td>商品分类名称</td>
						  <td>
							  <input type="hidden" name="parent_id" th:value="${parent_id}">
							  <input  class="form-control" name="name" placeholder="商品分类名称">
						  </td>
					  </tr>
					  <tr>
						  <td>类型模板</td>
						  <td>
							  <select name="typeTemplate.id" id="select_type_add" class="form-control" ></select>
							  <!--<input select2 config="options['type_template']" placeholder="商品类型模板" class="form-control" type="text"/>-->
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="addItemCat();">新增</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</form>
</body>
</html>